﻿//-----------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAPICodePack;
using System.Windows.Data;
using Microsoft.WindowsAPICodePack.Shell;

namespace Microsoft.LibraryManagerDemo.Presenters
{
    /// <summary>
    /// Provides logic for picking a <see cref="Microsoft.WindowsAPICodePack.StockIcon"/> icon.
    /// </summary>
    internal class IconPickerPresenter : PresenterBase
    {
        #region Properties

        /// <summary>
        /// Gets a list of system icons.
        /// </summary>
        public IList<StockIcon> IconStock { get; private set; }

        /// <summary>
        /// Gets the selected stock item identifier.
        /// </summary>
        public int SelectedIconIdentifier { get; private set; }

        #endregion

        #region Initializers

        /// <summary>
        /// Initialize a new instance of this type.
        /// </summary>
        public IconPickerPresenter()
        {
            InitializeIconStock();

            // Get te default icon collection view and select the first icon.
            var defaultView = CollectionViewSource.GetDefaultView(IconStock);
            defaultView.MoveCurrentTo(IconStock[0]);
            defaultView.CurrentChanged += defaultView_CurrentChanged;
        }

        #endregion

        #region Event Handlers

        void defaultView_CurrentChanged(object sender, EventArgs e)
        {
            var defaultView = CollectionViewSource.GetDefaultView(IconStock);
            var stockIcon = defaultView.CurrentItem as StockIcon;

            if (stockIcon != null)
            {
                // Update selected icon on icon selection change.
                SelectedIconIdentifier = (int)stockIcon.Identifier;
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Initializes the icon list with <see cref="Microsoft.WindowsAPICodePack.StockIcon"/>.
        /// </summary>
        private void InitializeIconStock()
        {
            IconStock = new List<StockIcon>();

            foreach (StockIconIdentifier iconId in Enum.GetValues(typeof(StockIconIdentifier)))
            {
                var stockIcon = new StockIcon(iconId, StockIconSize.Large, false, false);
                IconStock.Add(stockIcon);
            }
        }
        
        #endregion
    }
}
